<template>
  <div class="log-main">
    <div class="login-container">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <div class="logo">
            <img
              src="@/assets/logo.png"
              alt
            />
          </div>
          <h3 class="title">农业数字化管理平台</h3>
          <div class="tit-msg">Enterprise Data Service Solutions</div>
        </div>
        <div class="input_style">
          <el-form-item prop="account">
            <span class="svg-container">
              <!-- <svg-icon icon-class="user" /> -->
              <i
                class="el-icon-user"
                style="font-size: 26px"
              ></i>
            </span>
            <el-input
              ref="account"
              v-model="loginForm.account"
              placeholder="请输入用户名"
              name="account"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <i
                class="el-icon-lock"
                style="font-size: 26px"
              ></i>
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.enter.native="handleLogin(1)"
            />
            <span
              class="show-pwd"
              @click="showPwd"
            >
              <svg-icon
                style="color: rgba(0, 203, 134, 1)"
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>
          <el-form-item
            v-if="inputStuats === 1"
            prop="inputCode"
          >
            <div class="identifyCode">
              <el-input
                v-model="loginForm.inputCode"
                placeholder="请输入验证码"
                type="text"
                tabindex="1"
                autocomplete="on"
                @keyup.enter.native="handleLogin(1)"
              />
              <div
                class="identifyCode-main"
                @click="refreshCode()"
              >
                <SecurityCode
                  :identify-code="identifyCode"
                  :content-height="47"
                />
              </div>
            </div>
            <!-- <SliderCheck ref="slider" /> -->
            <!--验证码组件-->
          </el-form-item>
          <div class="new-msg">
            <div class="rember-msg">
              <el-checkbox
                v-model="remberMsg"
                label="记住密码"
              />
            </div>
            <div
              class="forget-msg"
              @click.stop="forget"
            >忘记密码</div>
          </div>
          <el-button
            :loading="loading"
            type="primary"
            style=" width: 100%;
              margin-bottom: 10px;
              background-color: rgba(127, 211, 84);
              height: 40px;
              font-size: 18px;
            "
            @click.native.prevent="handleLogin(1)"
          >登录</el-button>
        </div>
        <div
          class="reg-user"
          @click="toreguser()"
        >注册账户</div>
      </el-form>
    </div>
    <!-- <div class="copyright">
      版权所有：中科台达（浙江）信息技术有限公司｜ Copyright © 2019-2025
      casdelta.com All Right Reserved
      <br />
      <a
        href="https://beian.miit.gov.cn"
        target="view_window"
      >ICP备案号：浙ICP备19034790号-5</a>
    </div> -->
    <!-- 特殊 -->
    <!-- <div class="copyright">
      版权所有：COPYRIGHT © 渔雅科技（上海）有限公司
      <br />
      <a href="https://beian.miit.gov.cn" target="view_window">
        ICP备案号：沪ICP备20003371号-1</a>
    </div>-->
    <div class="future">
      <img
        src="@/assets/login/future.png"
        class="futureback"
        alt
      />
      <div class="futurecenter">Future</div>
    </div>

    <div class="smart">
      <img
        src="@/assets/login/smart.png"
        class="smartback"
        alt
      />
      <div class="smartcenter">Smart</div>
    </div>

    <div class="data">
      <img
        src="@/assets/login/data.png"
        class="databack"
        alt
      />
      <div class="datacenter">数字化</div>
    </div>

    <div class="weather">
      <img
        src="@/assets/login/weatherbor.png"
        class="weatherback"
        alt
      />
      <img
        src="@/assets/login/weather.png"
        class="weathercenter"
        alt
      />
    </div>
    <div class="tv">
      <img
        src="@/assets/login/tvbor.png"
        class="tvback"
        alt
      />
      <img
        src="@/assets/login/tv.png"
        class="tvcenter"
        alt
      />
    </div>

    <div class="green">
      <img
        src="@/assets/login/green.png"
        class="greenback"
        alt
      />
      <div class="greencenter">Green</div>
    </div>

    <div class="iot">
      <img
        src="@/assets/login/iot.png"
        class="iotback"
        alt
      />
      <div class="iotcenter">物联网</div>
    </div>
    <div class="qixiang">
      <img
        src="@/assets/login/qi.png"
        class="qiback"
        alt
      />
      <div class="qicenter">气象</div>
    </div>

    <div class="oc">
      <img
        src="@/assets/login/ocbor.png"
        class="ocback"
        alt
      />
      <img
        src="@/assets/login/oc.png"
        class="occenter"
        alt
      />
    </div>

    <div class="fisheries">
      <img
        src="@/assets/login/fisheries.png"
        class="fisheriesback"
        alt
      />
      <div class="fisheriescenter">Fisheries</div>
    </div>
  </div>
</template>

<script>
// import { validUsername } from "@/utils/validate";
import userService from "@/api/user/index";
// import SliderCheck from '@/components/securityCode/SliderCheck'
import { removeloToken, setloToken } from "@/utils/loginAuth";
import { setCookie, getCookie, getpasCookie } from "@/utils/cookie";

import SecurityCode from "@/components/securityCode/securityCode";
import { setToken } from "@/utils/auth";
export default {
  name: "Login",
  components: {
    SecurityCode
    // SliderCheck
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不得小于六位"));
      } else {
        callback();
      }
    };
    const validateinputCode = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入验证码（区分大小写）"));
      } else if (this.identifyCode !== this.loginForm.inputCode) {
        this.$set(this.loginForm, "inputCode", "");
        this.refreshCode();
        callback(new Error("请输入正确的验证码"));
      } else {
        callback();
      }
    };
    return {
      remberMsg: false,
      loginForm: {
        // account: "jg",
        // password: "123456"
        account: "",
        password: "",
        inputCode: undefined
      },
      loginRules: {
        account: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ],
        inputCode: [
          { required: true, validator: validateinputCode, trigger: "blur" }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      identifyCodeType: "1234567890", // 定义验证类型 1.数字 2.字母
      identifyCode: "",
      inputCode: "", // text框输入的验证码
      inputStuats: 2
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  mounted() {
    // 跳转过来时清除sessionStorage
    sessionStorage.clear();
    // location.reload();
    // 判断sessionStorage 有没有记住密码
    if (localStorage.remberMsg === "true") {
      this.remberMsg = true;
    }
    this.inputStuats = 1;
    this.getquery();
    this.refreshCode();
  },
  created() {
    if (this.$store.state.permission.addRoutes.length > 0) {
      // 判断是否已经刷新
      location.reload();
    } else {
      // location.reload();
    }
  },
  methods: {
    // 随机验证码
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    // 初始化验证码
    refreshCode() {
      this.identifyCode = ""; // 输入框置空
      this.makeCode(this.identifyCodeType, 4); // 验证码长度为4
    },
    // 随机切换验证码
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodeType[
          this.randomNum(0, this.identifyCodeType.length)
        ];
      }
      // console.log(this.identifyCode);
    },
    getquery() {
      let tr = this.$route.query;

      if (tr.organizationNo !== undefined) {
        setToken("organizationNo", tr.organizationNo);
      }
      if (tr.virtualSystemNo !== undefined) {
        setToken("virtualSystemNo", tr.virtualSystemNo);
      }
      if (tr.entrance_flag) {
        setToken("entrance_flag", tr.entrance_flag);
      }
      // console.log(this.inputStuats)
      if (tr.account) {
        // console.log(this)
        this.inputStuats = 2;
        // console.log(tr);
        this.loginForm.account = decodeURIComponent(tr.account);
        this.loginForm.password = tr.password;
        // if (tr.virtualSystemNo !== undefined) {
        //   setToken("virtualSystemNo", tr.virtualSystemNo);
        // }
        // this.loginForm.password = tr.password;
        setTimeout(() => {
          this.handleLogin(2);
        }, 200);
      } else if (tr.token) {
        // 避免明文 url 链接  // 但是基本上没用
        this.inputStuats = 2;
        let token = decodeURIComponent(
          window.location.href.split("?")[1].split("=")[1]
        ).replace(/ /g, "+");
        let obj = getpasCookie(token);
        this.loginForm.account = obj.account;
        this.loginForm.password = obj.password;
        setTimeout(() => {
          this.handleLogin(2);
        }, 200);
      } else if (this.remberMsg === true) {
        this.inputStuats = 1;
        // 从cookie 获取
        let loginForm = getCookie();
        this.$set(this.loginForm, "account", loginForm.account);
        this.$set(this.loginForm, "password", loginForm.password);
        // if (tr.virtualSystemNo !== undefined) {
        //   setToken("virtualSystemNo", tr.virtualSystemNo);
        // }
        // this.handleLogin(2);
        // this.inputStuats = 1;
      } else {
        this.inputStuats = 1;
      }
    },
    // 注册用户
    toreguser() {
      this.$router.push({ path: "/register-user" });
    },
    forget() {
      this.$router.push({ path: "/find-password" });
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin(e) {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          // 滑动验证码
          // if (this.$refs['slider']) {
          //   // 未通过验证时，提示错误信息并返回
          //   if (!this.$refs['slider'].confirmSuccess) {
          //     this.$message.error("请拖动滑块验证");
          //     return
          //   }
          // }
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.loading = false;
              // 存储加密账号密码到cookie
              if (this.remberMsg) {
                setCookie(this.loginForm.account, this.loginForm.password, 15);
                setloToken("remberMsg", this.remberMsg);
              } else {
                removeloToken("remberMsg");
              }
              // this.$router.push({ path: this.redirect || "/" });
              this.$router.push({ path: "/choose-company" });
            })
            .catch(err => {
              this.$message.warning(
                err ||
                  "用户账号/密码错误(或已被加入黑名单),请确认后重试或联系管理员"
              );
              this.loading = false;
            });
        } else {
          console.log("表单提交错误!!");
          return false;
        }
      });
    },
    // 主要用于登录完判断进入哪个页面--暂时没什么用
    gotoview() {
      // 获取顶级组织结构id,
      userService.organizationTop().then(res => {
        let companylist = res.data;
        alert(companylist.length);
        if (companylist.length > 1) {
          this.$router.push({
            path: "/select-farm",
            query: { id: companylist[0]["companyId"] }
          });
        } else if (companylist.length === 1) {
          this.$router.push({
            path: "/choose-company",
            query: { id: companylist[0]["companyId"] }
          });
        }
      });

      this.loading = false;
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* 登录主体样式 */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      appearance: none;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.log-main {
  width: 100%;
  height: 100%;
  background: linear-gradient(
    150.62deg,
    rgba(0, 203, 134, 1) 0%,
    rgba(31, 124, 135, 1) 100%,
    rgba(26, 135, 133, 1) 100%
  );
  overflow: hidden;
  position: relative;
  // overflow:auto;
  // overflow: hidden;
  // overflow-y: hidden;
  // overflow-x: hidden;
  // -ms-overflow-style: auto;
}
// .log-main::-webkit-scrollbar { width: 0 !important }
.identifyCode {
  width: 100%;
  display: flex;
  .identifyCode-main {
    height: 47px;
  }
}
.login-container {
  min-height: 100%;
  width: 100%;
  background-size: 100% 100% !important;
  background-attachment: fixed !important;
  background: url("~@/assets/login/back.png") center center no-repeat;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-form {
    overflow: hidden;
    position: relative;
    width: 565px;
    max-width: 100%;
    top: -20px;
    margin: auto;
    // overflow: hidden;
    background-color: rgba(0, 81, 81, 0.8);
    z-index: 99;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    margin-top: 28px;
    position: relative;
    top: 18px;
    .logo {
      text-align: center;
      // width: 120px;
      img {
        width: 120px;
      }
    }
    .title {
      font-size: 26px;
      color: #fff;
      margin: 20px auto 2px auto;
      text-align: center;
      font-weight: bold;
    }
    .tit-msg {
      color: rgba(255, 255, 255, 0.56);
      text-align: center;
      margin-bottom: 35px;
    }
  }
  .input_style {
    padding: 0 35px;
    box-sizing: border-box;
    .svg-container {
      color: rgba(127, 211, 84);
    }
    ::v-deep .el-input input {
      caret-color: #7fd354;
      margin-left: 20px;
      outline: none !important;
      border: none !important;
      padding: 0 !important;
      color: $bg;
    }
    // 自动填充样式input,真恶心
    ::v-deep .el-input {
      .el-input__inner:-webkit-autofill,
      .el-input__inner:-webkit-autofill:hover,
      .el-input__inner:-webkit-autofill:active,
      .el-input__inner:-webkit-autofill {
        margin-left: 20px;
        outline: none !important;
        border: none !important;
        padding: 0 !important;
        -webkit-box-shadow: 0 0 0px 1000px rgb(246, 246, 246) inset !important;
        box-shadow: 0 0 0px 1000px rgb(246, 246, 246) inset !important;
        -webkit-text-fill-color: #2d3a4b !important;
      }
    }
    ::v-deep .el-form-item__content {
      border: 1px solid rgba(217, 217, 217, 1);
      border-radius: 2px;
      background-color: #fff;
    }
    // 记住密码与忘记密码
    .new-msg {
      display: flex;
      margin-bottom: 20px;
      width: 525px;
      justify-content: space-between;
      .rember-msg {
        width: 80px;
        height: 22px;
        color: rgba(255, 255, 255, 0.65);
        font-size: 14px;
        line-height: 22px;
        .el-checkbox {
          color: rgba(255, 255, 255, 0.65);
          font-size: 14px;
        }
        ::v-deep .el-checkbox__input.is-checked + .el-checkbox__label {
          color: #7fd354;
        }
        ::v-deep .el-checkbox__input.is-checked .el-checkbox__inner,
        .el-checkbox__input.is-indeterminate .el-checkbox__inner {
          background-color: #7fd354;
          border-color: #7fd354;
        }
      }
      .forget-msg {
        margin-right: 35px;
        width: 56px;
        height: 22px;
        color: #7fd354;
        font-size: 14px;
        line-height: 22px;
        cursor: pointer;
      }
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .reg-user {
    width: 565px;
    text-align: center;
    height: 22px;
    color: rgba(127, 211, 84, 1);
    font-size: 14px;
    line-height: 22px;
    cursor: pointer;
    margin-bottom: 20px;
  }
}
</style>
<style lang="scss" scoped>
.copyright {
  width: 100%;
  position: absolute;
  bottom: 0px;
  text-align: center;
  color: white;
  line-height: 2;
  font-size: 10px;
  margin-bottom: 0;
  background-color: rgba(32, 3, 4, 0.35);
}

.future {
  position: absolute;
  top: 23%;
  left: 27%;
  display: flex;
  justify-content: center;
  align-items: center;
  .futureback {
    width: 270px;
    position: absolute;
    animation: upmove 15s linear infinite;
    -moz-animation: upmove 15s linear infinite; /* Firefox */
    -webkit-animation: upmove 15s linear infinite; /* Safari and Chrome */
    -o-animation: upmove 15s linear infinite; /* Opera */
  }
  .futurecenter {
    font-size: 42px;
    color: #fff;
  }
}
.smart {
  position: absolute;
  top: 69%;
  left: 20%;
  display: flex;
  justify-content: center;
  align-items: center;
  .smartback {
    width: 300px;
    position: absolute;
    animation: upmove 8s linear infinite;
    -moz-animation: upmove 8s linear infinite; /* Firefox */
    -webkit-animation: upmove 8s linear infinite; /* Safari and Chrome */
    -o-animation: upmove 8s linear infinite; /* Opera */
  }
  .smartcenter {
    font-size: 35px;
    color: #fff;
  }
}

.data {
  position: absolute;
  top: 72%;
  left: 75%;
  display: flex;
  justify-content: center;
  align-items: center;
  .databack {
    width: 350px;
    position: absolute;
    animation: upmove 10s linear infinite;
    -moz-animation: upmove 10s linear infinite; /* Firefox */
    -webkit-animation: upmove 10s linear infinite; /* Safari and Chrome */
    -o-animation: upmove 10s linear infinite; /* Opera */
  }
  .datacenter {
    font-size: 35px;
    color: #fff;
  }
}
.weather {
  position: absolute;
  top: 45%;
  left: 82.1%;
  display: flex;
  justify-content: center;
  align-items: center;
  .weatherback {
    width: 210px;
    position: absolute;
    animation: upmove 8s linear infinite;
    -moz-animation: upmove 8s linear infinite; /* Firefox */
    -webkit-animation: upmove 8s linear infinite; /* Safari and Chrome */
    -o-animation: upmove 8s linear infinite; /* Opera */
  }
  .weathercenter {
    color: #fff;
  }
}
.tv {
  position: absolute;
  top: 82%;
  left: 38%;
  display: flex;
  justify-content: center;
  align-items: center;
  .tvback {
    width: 160px;
    position: absolute;
    animation: mymove 5s ease-in-out infinite;
    -moz-animation: mymove 5s ease-in-out infinite; /* Firefox */
    -webkit-animation: mymove 5s ease-in-out infinite; /* Safari and Chrome */
    -o-animation: mymove 5s ease-in-out infinite; /* Opera */
  }
}

.green {
  position: absolute;
  top: 23%;
  left: 88%;
  display: flex;
  justify-content: center;
  align-items: center;
  .greenback {
    width: 180px;
    position: absolute;
    animation: upmove 8s linear infinite;
    -moz-animation: upmove 8s linear infinite; /* Firefox */
    -webkit-animation: upmove 8s linear infinite; /* Safari and Chrome */
    -o-animation: upmove 8s linear infinite; /* Opera */
  }
  .greencenter {
    font-size: 25px;
    color: #fff;
  }
}

.iot {
  position: absolute;
  top: 16%;
  left: 66%;
  display: flex;
  justify-content: center;
  align-items: center;
  .iotback {
    width: 185px;
    position: absolute;
    animation: upmove 15s linear infinite;
    -moz-animation: upmove 15s linear infinite; /* Firefox */
    -webkit-animation: upmove 15s linear infinite; /* Safari and Chrome */
    -o-animation: upmove 15s linear infinite; /* Opera */
  }
  .iotcenter {
    font-size: 30px;
    color: #fff;
  }
}
.qixiang {
  position: absolute;
  top: 79%;
  left: 12%;
  display: flex;
  justify-content: center;
  align-items: center;
  .qiback {
    width: 110px;
    position: absolute;
    animation: upmove 15s linear infinite;
    -moz-animation: upmove 15s linear infinite; /* Firefox */
    -webkit-animation: upmove 15s linear infinite; /* Safari and Chrome */
    -o-animation: upmove 15s linear infinite; /* Opera */
  }
  .qicenter {
    font-size: 20px;
    color: #fff;
  }
}

.oc {
  position: absolute;
  top: 18%;
  left: 10%;
  display: flex;
  justify-content: center;
  align-items: center;
  .ocback {
    width: 70px;
    position: absolute;
    animation: mymove 5s ease-in-out infinite;
    -moz-animation: mymove 5s ease-in-out infinite; /* Firefox */
    -webkit-animation: mymove 5s ease-in-out infinite; /* Safari and Chrome */
    -o-animation: mymove 5s ease-in-out infinite; /* Opera */
  }
}

.fisheries {
  position: absolute;
  top: 40%;
  left: 9%;
  display: flex;
  justify-content: center;
  align-items: center;
  .fisheriesback {
    width: 180px;
    position: absolute;
    animation: mymove 5s ease-in-out infinite;
    -moz-animation: mymove 5s ease-in-out infinite; /* Firefox */
    -webkit-animation: mymove 5s ease-in-out infinite; /* Safari and Chrome */
    -o-animation: mymove 5s ease-in-out infinite; /* Opera */
  }
  .fisheriescenter {
    font-size: 24px;
    color: #fff;
  }
}
@keyframes upmove {
  0% {
    transform: rotateZ(0deg);
    -webkit-transform: rotateZ(0deg);
    -moz-transform: rotateZ(0deg);
    -o-transform: rotateZ(0deg);
    -ms-transform: rotateZ(0deg);
  }
  100% {
    transform: rotateZ(360deg);
    -webkit-transform: rotateZ(360deg);
    -moz-transform: rotateZ(360deg);
    -o-transform: rotateZ(360deg);
    -ms-transform: rotateZ(360deg);
  }
}
@keyframes mymove {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    //  background-position: 100% 0%;
    transform: scale(1.2);
    opacity: 1;
  }
  100% {
    //  background-position: 100% 0%;
    transform: scale(1);
    opacity: 1;
  }
}
</style>
